﻿@using CJAtech.BBS.DataEntity;
@using TjZheng.Domain.DataEntity;
@using CJAtech.BBS.Process;
@using TjZheng.CMS.Areas.BBS.Models;
@inherits CMSWebViewPage<CJ_VoteInfo>
@{
    ViewBag.Title = "投票信息编辑";
    Layout = "~/Views/Shared/_EditLayout.cshtml";
    DbSetInfo objDbSetInfo = ViewBag.DbSetInfo as DbSetInfo;
}
@section Head{
    @Html.CreateEditorSource()
}
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false" class="xhidden">
        <div class="top-block">
            @CurrentButtonTool.Create()
        </div>
        <form id="fr_voteinfo" method="post" enctype="multipart/form-data">
            @*@Html.AntiForgeryToken()*@
            <table class="tblContent">
                <colgroup>
                    <col class="colTitle" />
                    <col class="colContent" />
                    <col class="colTitle" />
                    <col class="colContent" />
                </colgroup>
                <tr>
                    <td><span class="txtNoNull">*</span>投票标题:</td>
                    <td colspan="3">
                        @Html.Hidden("VoteInfo.VoteHeadID", Model.VoteHeadID, false)
                        @Html.Hidden("VoteInfo.ArticleID", Model.ArticleID, false)
                        @Html.StringBox("VoteInfo.VoteTitle", Model.VoteTitle, true, "请输入投票标题", new { style = "width:600px;" }, ValidType.Length(50))
                    </td>
                </tr>

                <tr>
                    <td><span class="txtNoNull">*</span>投票选项内容类型:</td>
                    <td>
                        @Html.ComboBox("VoteInfo.ContentType", Model.ContentType, true, "请选择投票选项类型", new List<SelectListItem>{
                           new SelectListItem{Text="文字",Value="0"},
                           new SelectListItem{Text="图片",Value="1"},
                           new SelectListItem{Text="视频",Value="2"},
                           new SelectListItem{Text="图片+文字",Value="3"},
                           new SelectListItem{Text="视频+文字",Value="4"}
                        })
                    </td>
                    <td><span class="txtNoNull">*</span>最大勾选数量:</td>
                    <td>

                        @Html.NumberBox("VoteInfo.MaxCheckNum", Model.MaxCheckNum, new { style = "width:200px;" }, 0, 50)
                        @*@Html.StringBox("objProvide.CommentCount", Model.CommentCount, false, "请输入评论数", null, ValidType.Length(10))*@
                    </td>
                </tr>

                <tr>
                    <td>投票选项：</td>
                    <td colspan="3">
                        <input type="button" value="添加投票选项" class="addDetail" />
                        @*<input type="button" value="上移" onclick="MoveUpPicture();" />
                            <input type="button" value="下移" onclick="MoveDownPicture();" />*@
                        <ul id="ul_articlepicturecollection">
                            @if (Model.DetailList != null && Model.DetailList.Count != 0)
                            {
                                for (var i = 0; i < Model.DetailList.Count; i++)
                                {
                                    var detail = Model.DetailList[i];
                                    <li style="background-color:#ccc;margin-top:8px;padding:10px;">

                                        <p>投票选项：<input style="margin-left:265px;" type="button" class="delVoteDetail" value="删除该投票选项" /></p>
                                        <p>文本内容：@Html.StringBox("VoteInfo.DetailList[" + i + "].TextContent", detail.TextContent, false, "请输入文本内容")</p>
                                        <ul>
                                            @Html.Hidden("VoteInfo.DetailList[" + i + "].VoteDetailID", detail.VoteDetailID, false)
                                            上传资源：
                                            @for (var j = 0; j < detail.ResourceContentList.Count; j++)
                                            {
                                                <li style="margin-left:60px">
                                                    <input class="hid_val" type="hidden" />
                                                    @Html.ImagePreview("PictureUrl", detail.ResourceContentList[j], new { style = "width:30px;height:30px;" })
                                                    @Html.UpLoadNativeImage(8, "PictureUrl", detail.ResourceContentList[j], "PictureCollegePreview(this)")
                                                    @*<div id="preview" style="width: 300px;height:300px;border:1px solid gray;"></div>*@
                                                    <input type="button" value="添加" class="add" />
                                                    <input type="button" value="删除" class="remove" />
                                                </li>
                                            }
                                        </ul>

                                    </li>
                                }
                            }
                        </ul>
                    </td>
                </tr>

            </table>
        </form>
    </div>

</div>

<div id="temp" style="display:none">
    <li style="margin-left:60px">
        <input class="hid_val" type="hidden" />
        @Html.ImagePreview("PictureUrl", "", new { style = "width:30px;height:30px;" })
        @Html.UpLoadNativeImage(8, "PictureUrl", "", "PictureCollegePreview(this)")
        @*<div id="preview" style="width:30px;height:30px;border:1px solid gray;overflow :hidden;"></div>*@
        <input type="button" value="添加" class="add" />
        <input type="button" value="删除" class="remove" />
    </li>
</div>
<div id="tempDetail" style="display:none">
    <li style="background-color:#ccc;margin-top:8px;padding:10px;">
        <p>投票选项：<input style="margin-left:265px;" type="button" class="delVoteDetail" value="删除该投票选项" /></p>
        <p> 文本内容：<input type="text" /></p>
        <ul>
            上传资源：
            <li style="margin-left:60px">
                <input class="hid_val" type="hidden" />
                @Html.ImagePreview("PictureUrl", "", new { style = "width:30px;height:30px;" })
                @Html.UpLoadNativeImage(8, "PictureUrl", "", "PictureCollegePreview(this)")
                @*<div class="preview" style="width: 300px;height:300px;border:1px solid gray;"></div>*@
                <input type="button" value="添加" class="add" />
                <input type="button" value="删除" class="remove" />
            </li>
        </ul>
    </li>
</div>
@section Bottom{
    <script type="text/javascript">
        function Save() {
            var type = $("#VoteInfo_ContentType").val();
            var msg = "";
            if (type != 1 && type != 2) {
                $("#ul_articlepicturecollection").find(":text").each(function () {
                    var val = $(this).val();
                    if (val == "") {
                        msg = "请输入文本内容！";
                        return false;
                    }
                })
            }
            if (type != 0) {
                $("#ul_articlepicturecollection").find(".hid_val").each(function () {
                    var val = $(this).val();
                    if (val == "") {
                        msg = "请选择上传文件！";
                        return false;
                    }
                })
            }
            if (msg != "") {
                ShowMessage(msg); return false;
            }
            var objFormEntity = new FormEntity("#fr_voteinfo");
            objFormEntity.url = "/bbs/VoteInfo/save?SetId=@objDbSetInfo.SetId";
            objFormEntity.submitSuccess = function (InvokeResult) {
                if (InvokeResult.ResultCode == "Success") {
                    ShowMessage(InvokeResult.ResultMessage);
                    Back();
                } else {
                    ShowMessage(InvokeResult.ResultMessage);
                }
            };
            objFormEntity.isClearData = false;
            objFormEntity.submit();
        }
        function Back() {
            TransferToUrl("/bbs/VoteInfo/Index?SetId=@objDbSetInfo.SetId");
        }
        $(".add").live("click", function () {
            var html = $("#temp").html();
            $(this).parent().parent().append(html); sort()
        })
        $(".remove").live("click", function () {
            var type = $("#VoteInfo_ContentType").val();
            if (type != 0) {
                var length = $(this).parent().parent().find("li").length;
                if (length != 1) {
                    $(this).parent().remove(); sort();
                }
            }
        })
        $(".delVoteDetail").live("click", function () {
            var len = $(this).parent().parent().parent().children("li").length;
            if (len != 1) {
                $(this).parent().parent().remove(); sort();
            }
        })
        $(".addDetail").bind("click", function () {
            var html = $("#tempDetail").html();
            $("#ul_articlepicturecollection").append(html); sort()
        })
        function AddPicture() {
            var html = $("#tempDetail").html();
            $("#ul_articlepicturecollection").append(html); sort()
        }
        function sort() {
            $("#ul_articlepicturecollection>li").each(function (i, val) {
                $(this).find(":text").attr("name", "VoteInfo.DetailList[" + i + "].TextContent");
                $(this).find(".hid_VoteDetail").attr("name", "VoteInfo.DetailList[" + i + "].VoteDetailID");
                $(this).find("li").each(function (j, vl) {
                    $(this).find(".hid_val").attr("name", "VoteInfo.DetailList[" + i + "].ResourceContentList[" + j + "]");
                    $(this).find(".add").css("display", "none");
                    $(this).parent().find(".add").last().css("display", "inline-block");
                })
            })
            initType()
        }
        function initType() {
            var val = $("#VoteInfo_ContentType").val();
            if (val == 0 || val == 3 || val == 4) {
                $("#ul_articlepicturecollection").find(":text").parent().show();
            } else {
                $("#ul_articlepicturecollection").find(":text").parent().hide();
            }
            if (val != 0) {
                $("#ul_articlepicturecollection").find("ul").show();
            }
            else {
                $("#ul_articlepicturecollection").find("ul").hide();
            }
        }
        $("#VoteInfo_ContentType").bind("change", function () {
            initType();
        })
        $(function () {
            $('[type=file]').live("change", function (e) {
                var file = e.target.files[0];
                $(this).parent().find(".hid_val").val(file.name);
            })
            $(".hid_val").each(function () {
                var picVal = $(this).parent().find("#PictureUrl").first().val();
                $(this).val(picVal);
            })
            $("#VoteInfo_ArticleID").val(@TransferThreeID);
            $("#ul_articlepicturecollection ul").each(function () {
                $(this).find(":hidden").first().addClass("hid_VoteDetail");
            })
            sort();
        })
        //本地上传图片预览
        function PictureCollegePreview(source) {
            var $img = $(source).parent().find("img");
            if ($.browser.msie && ($.browser.version == "7.0" || $.browser.version == "8.0")) {
                var src = $(source).val();
                $img.attr("src", "file:///" + src);
                var title = src.substring(src.lastIndexOf("\\") + 1);
                $img.attr("title", title);
                $img.tooltip({ content: '<span>' + title + '</span>', });
            } else {
                var objFile = source.files[0];//选择上传的文件
                var objFileReader = new FileReader();
                objFileReader.readAsDataURL(objFile);//Base64
                $(objFileReader).load(function () {
                    $img.attr("src", this.result);
                    $img.attr("title", objFile.name);
                    $img.tooltip({ content: '<span>' + objFile.name + '</span>', });
                });
            }

        }
    </script>
}
